package com.ponhu.ea2.stockcenter.provider.service.entity.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ponhu.ea2.stockcenter.provider.entity.StockWarehouseRelation;
import com.ponhu.ea2.stockcenter.provider.mapper.StockWarehouseRelationMapper;
import com.ponhu.ea2.stockcenter.provider.service.entity.StockWarehouseRelationService;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 货品仓库库存表服务实现类
 */
@Service
public class StockWarehouseRelationServiceImpl
        extends ServiceImpl<StockWarehouseRelationMapper, StockWarehouseRelation>
        implements StockWarehouseRelationService {


    @Autowired
    private StockWarehouseRelationMapper stockWarehouseRelationMapper;

    /**
     * 根据货品编码和仓库编码查询货品仓库库存信息
     *
     * @param inventoryCodeList
     * @return
     */
    @Override
    public List<StockWarehouseRelation> getStockWarehouseRelations(Collection<String> inventoryCodeList, String warehouseCode) {

        return getStockWarehouseRelations(inventoryCodeList, warehouseCode, null);
    }

    /**
     * 根据货品编码和仓库编码查询货品仓库库存信息
     *
     * @param inventoryCodeList
     * @return
     */
    @Override
    public List<StockWarehouseRelation> getStockWarehouseRelations(Collection<String> inventoryCodeList, String warehouseCode, Boolean hasStock) {

        if (CollectionUtils.isEmpty(inventoryCodeList) && StringUtils.isEmpty(warehouseCode))
            return Lists.newArrayList();

        LambdaQueryWrapper<StockWarehouseRelation> queryWrapper = Wrappers
                .lambdaQuery(StockWarehouseRelation.class);

        if (CollectionUtils.isNotEmpty(inventoryCodeList))
            queryWrapper.in(StockWarehouseRelation::getInventoryCode, inventoryCodeList);

        if (StringUtils.isNotBlank(warehouseCode))
            queryWrapper.eq(StockWarehouseRelation::getWarehouseCode, warehouseCode);

        if (BooleanUtils.isTrue(hasStock))
            queryWrapper.gt(StockWarehouseRelation::getRealStockNum, 0);

        return list(queryWrapper);
    }

    /**
     * 根据货品编码查询货品仓库库存信息
     *
     * @param inventoryCodeList
     * @return
     */
    @Override
    public List<StockWarehouseRelation> getRelationsByInventoryCodes(Collection<String> inventoryCodeList) {

        return getStockWarehouseRelations(inventoryCodeList, null);
    }

    @Override
    public Integer getTotalRealStockNumByInventoryCode(String inventoryCode) {
        Integer totalRealStockNum = stockWarehouseRelationMapper.getTotalRealStockNumByInventoryCode(inventoryCode);
        return totalRealStockNum == null ? 0 : totalRealStockNum;
    }

    /**
     * 根据仓库编码查询货品仓库库存信息
     *
     * @param warehouseCode
     * @return
     */
    @Override
    public List<StockWarehouseRelation> getRelationsByWarehouseCode(String warehouseCode, Boolean hasStock) {

        return getStockWarehouseRelations(null, warehouseCode, hasStock);
    }

}




